package com.wn.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wn.entity.Dept;
import com.wn.service.DeptService;
import com.wn.util.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Gapper
 * @since 2023-02-09
 */
@RestController
@RequestMapping("/dept")
public class DeptController {
    @Resource
    private DeptService deptService;

    @PostMapping("search/{keyWord}")
    public Result search(@PathVariable String keyWord){
        Page<Dept> deptPage = new Page<>(1,2);
        Page<Dept> page = deptService.page(deptPage);
        deptService.page(page, Wrappers.<Dept>lambdaQuery().like(Dept::getDeptName,"%" + keyWord));
        System.out.println("1111");
        return Result.ok("查询成功").data("pageinfo",page);

    }

    @PostMapping("{deptId}")
    public Result selectOne(@PathVariable Integer deptId){
        Dept dept = deptService.getById(deptId);
        return Result.ok("查询成功").data("dept",dept);
    }

    @PostMapping("queryAll")
    public Result queryAll(){
        Page<Dept> deptPage = new Page<>(1,2);
        Page<Dept> page = deptService.page(deptPage);
        List<Dept> list = page.getRecords();

        //page中有封装好的各种数据如下注释所示
//        "page": {
//            "records": [
//            {
//                "deptId": 1,
//                    "deptName": "开发"
//            },
//            {
//                "deptId": 2,
//                    "deptName": "测试"
//            }
//			],
//            "total": 3,
//                    "size": 2,
//                    "current": 1,
//                    "orders": [],
//            "optimizeCountSql": true,
//                    "hitCount": false,
//                    "countId": null,
//                    "maxLimit": null,
//                    "searchCount": true,
//                    "pages": 2
//        },

        return Result.ok("查询成功").data("depts",list).data("page",page);
    }

    @PostMapping("/add")
    public Result add(@RequestBody Dept dept) {
        deptService.save(dept);
        return Result.ok("");
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam int deptId) {
        deptService.removeById(deptId);
        return Result.ok("删除成功！");
    }

    @PostMapping("/update")
    public Result update(@RequestBody Dept dept) {
        deptService.updateById(dept);
        return Result.ok("更新成功！");
    }
/**
 * 测试
 */
public String getApp(){
    return "ok";
}
}

